package com.fwtai.api;

import com.fwtai.bean.PageFormData;
import com.fwtai.datasource.DaoHandle;
import com.fwtai.tool.IdBuilder;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 案件|案源访问数据库
 * @作者 田应平
 * @版本 v1.0
 * @创建时间 2021-07-28 12:09:11
 * @QQ号码 444141300
 * @Email service@yinlz.com
 * @官网 <url>http://www.fwtai.com</url>
*/
@Repository
public class ApiLegalCaseDao{

    @Resource
    private DaoHandle dao;

    public int add(final PageFormData formData){
        return dao.execute("api_legal_case.add",formData);
    }

    /**律师自己添加自己的案件,默认已委托且已签订协议*/
    @Transactional
    public int addForLawyer(final PageFormData formData){
      final String bailor = formData.getString("bailor");
      if(bailor != null){
        final ArrayList<HashMap<String,Object>> list = new ArrayList<>();
        final String[] userIds = bailor.split(",");
        final Object kid = formData.get("kid");
        for(int i = userIds.length - 1; i >= 0; i--){
          final HashMap<String,Object> map = new HashMap<>(2);
          map.put("caseId",kid);
          map.put("userId",userIds[i]);
          map.put("legal_relation_key",new IdBuilder().getId121L());
          list.add(map);
        }
        if(list.size() > 0){
          dao.execute("api_legal_case.addLawyerCase",list);//律师发布自己之前的案件并绑定当事人
        }
      }
      formData.put("lawyerAddId",new IdBuilder().getId121L());
      dao.execute("api_legal_case.addCaseStatus",formData);//律师发布时默认为已委托状态
      formData.put("lawyer",1);
      formData.put("my_case_key",new IdBuilder().getId121L());
      formData.put("legal_case_accept_key",new IdBuilder().getId121L());
      dao.execute("api_legal_case.addMyCase",formData);//律师发布时默认为已签订协议
      dao.execute("api_legal_case.addCaseAccept",formData);//律师发布案件时默认为已确认为最终委托人
      return dao.execute("api_legal_case.add",formData);
    }

    /**律师删除自己的案件*/
    @Transactional
    public int delCaseAll(final PageFormData formData){
        dao.execute("api_legal_case.delLawyerCaseStatus",formData);
        dao.execute("api_legal_case.delLegalRelation",formData);
        dao.execute("api_legal_case.delLawyerMyCase",formData);
        dao.execute("api_legal_case.delAcceptLawyer",formData);
        return dao.execute("api_legal_case.delLawyerCaseInfo",formData);
    }

    public String queryExistById(final String kid){
        return dao.queryForString("api_legal_case.queryExistById",kid);
    }

    public String queryExistCaseStatus(final String kid){
        return dao.queryForString("api_legal_case.queryExistCaseStatus",kid);
    }

    public int edit(final PageFormData formData){
        return dao.execute("api_legal_case.edit",formData);
    }

    public int addStatus(final PageFormData formData){
        return dao.execute("api_legal_case.addStatus",formData);
    }

    public int updateCaseStatus(final PageFormData formData){
        return dao.execute("api_legal_case.updateCaseStatus",formData);
    }

    public HashMap<String, Object> queryById(final String kid){
        return dao.queryForHashMap("api_legal_case.queryById",kid);
    }

    public HashMap<String, Object> queryCaseInfoById(final PageFormData formData){
        return dao.queryForHashMap("api_legal_case.queryCaseInfoById",formData);
    }

    /**案件发布人删除自己的案件*/
    @Transactional
    public int delById(final PageFormData formData){
        dao.execute("api_legal_case.delCaseAccept",formData);//删除预委托
        dao.execute("api_legal_case.del_case_status",formData);//删除案件状态
        return dao.execute("api_legal_case.delById",formData);//最后删除案件本身
    }

    public int delCaseStatus(final PageFormData formData){
        return dao.execute("api_legal_case.delCaseStatus",formData);
    }

    public int updatePreAccept(final PageFormData formData){
        return dao.execute("api_legal_case.updatePreAccept",formData);
    }

    @Transactional
    public int addAccept(final PageFormData formData){
        dao.execute("api_legal_case.legalCaseStatus",formData);
        dao.execute("api_legal_case.updateAcceptStatus",formData);
        return dao.execute("api_legal_case.addAccept",formData);
    }

    //律师取消案件的委托
    @Transactional
    public int delEntrust(final PageFormData formData){
        dao.execute("api_legal_case.delEntrustAccept",formData);//删除预委托
        dao.execute("api_legal_case.delEntrustStatus",formData);//删除案件状态
        return dao.execute("api_legal_case.delEntrustCase",formData);//删除已委托的案件
    }

    /**协议签订*/
    @Transactional
    public int addAgreement(final PageFormData formData){
        final String bailor = formData.getString("bailor");
        if(bailor != null){
            final String[] userIds = bailor.split(",");
            final Object caseId = formData.get("case_id");
            final ArrayList<HashMap<String,Object>> list = new ArrayList<>();
            for(int i = userIds.length - 1; i >= 0; i--){
                final HashMap<String,Object> map = new HashMap<>(3);
                map.put("userId",userIds[i]);
                map.put("caseId",caseId);
                map.put("legal_relation_key",new IdBuilder().getId121L());
                list.add(map);
            }
            if(list.size() > 0){
                dao.execute("api_legal_case.addLawyerCase",list);
            }
        }
        dao.execute("api_legal_case.updateAgreement",formData);
        return dao.execute("api_legal_case.addAgreement",formData);
    }

    public int delByKeys(final ArrayList<String> list){
        return dao.execute("api_legal_case.delByKeys",list);
    }

    public int addPreAccept(final PageFormData formData){
        return dao.execute("api_legal_case.addPreAccept",formData);
    }

    public List<Map<String,Object>> listData(final PageFormData formData){
        return dao.queryForListMap("api_legal_case.listData",formData);
    }

    /**案源广场*/
    public List<Map<String,Object>> listCaseMaidan(final PageFormData formData){
        return dao.queryForListMap("api_legal_case.listCaseMaidan",formData);
    }

    /**用户查看自己的案件*/
    public List<Map<String,Object>> listCaseSelf(final PageFormData formData){
        return dao.queryForListMap("api_legal_case.listCaseSelf",formData);
    }

    /**律师查看自己的案件,含自己发布的案件和自己已接的案件*/
    public List<Map<String,Object>> listCaseLawyer(final PageFormData formData){
        return dao.queryForListMap("api_legal_case.listCaseLawyer",formData);
    }

    public HashMap<String,Object> listPreAccept(final PageFormData formData){
        return dao.queryForPage(formData,"api_legal_case.listPreData","api_legal_case.listPreTotal");
    }

    public List<Map<String,Object>> listDataFilter(final PageFormData formData){
        return dao.queryForListMap("api_legal_case.listDataFilter",formData);
    }

    public List<Map<String,Object>> queryLawyerPreCase(final PageFormData formData){
        return dao.queryForListMap("api_legal_case.listDataPreLawyerCase",formData);
    }

    public List<Map<String,Object>> queryStatusDetails(final String statusId){
        return dao.queryForListMap("api_legal_case.queryStatusDetails",statusId);
    }

    public List<Map<String,Object>> queryDetailsAttach(final String detailsKid){
        return dao.queryForListMap("api_legal_case.queryDetailsAttach",detailsKid);
    }

    public String queryAcceptTotal(final String kid){
        return dao.queryForString("api_legal_case.queryAcceptTotal",kid);
    }

    public Integer queryAgreement(final String kid){
        return dao.queryForInteger("api_legal_case.queryAgreement",kid);
    }

    public String queryOwnCase(final PageFormData formData){
        return dao.queryForString("api_legal_case.queryOwnCase",formData);
    }

    public List<Map<String, Object>> queryStatusList(final PageFormData formData){
        return dao.queryForListMap("api_legal_case.queryStatusList",formData);
    }

    public HashMap<String,Object> getAgreement(final PageFormData formData){
        return dao.queryForHashMap("api_legal_case.getAgreement",formData);
    }

    @Transactional
    public int addStatusDetails(final PageFormData formData,final ArrayList<HashMap<String,String>> listAttach,final ArrayList<HashMap<String,String>> listImg){
        if(listAttach.size() > 0){
            dao.execute("api_legal_case.addDetailsAttach",listAttach);
        }
        if(listImg.size() > 0){
            dao.execute("api_legal_case.addDetailsImg",listImg);
        }
        return dao.execute("api_legal_case.addStatusDetails",formData);
    }

    /**
    * 仅从已委托列表里移除即可
    */
    @Transactional
    public int delPreAccept(final PageFormData formData){
        dao.execute("api_legal_case.delPreCaseStatus",formData);
        dao.execute("api_legal_case.delPreCaseAccept",formData);
        return dao.execute("api_legal_case.delPreMyCase",formData);
    }

    /**删除案件状态明细详情列表项*/
    public int delStatusDetails(final PageFormData formData){
        return dao.execute("api_legal_case.delStatusDetails",formData);
    }

    public String queryAffirm(final PageFormData formData){
        return dao.queryForString("api_legal_case.queryAffirm",formData);
    }

    public Integer queryCasedetails(final String caseId){
        return dao.queryForInteger("api_legal_case.queryCasedetails",caseId);
    }

    public List<Map<String, Object>> listLawOfficeUser(final String loginId){
        return dao.queryForListMap("api_legal_case.listLawOfficeUser",loginId);
    }

    public String queryCaseOwner(final PageFormData formData){
        return dao.queryForString("api_legal_case.queryCaseOwner",formData);
    }

    public List<Map<String,Object>> getCaseParty(final String kid){
        return dao.queryForListMap("api_legal_case.getCaseParty",kid);
    }

    public String queryCaseSelf(final PageFormData formData){
        return dao.queryForString("api_legal_case.queryCaseSelf",formData);
    }

    public int delCancelPreCase(final PageFormData formData){
        return dao.execute("api_legal_case.delCancelPreCase",formData);
    }

    /**仅查询案件是否存在*/
    public String getCaseExist(final String kid){
        return dao.queryForString("api_legal_case.getCaseExist",kid);
    }

    public int addCaseMaterial(final PageFormData formData){
        return dao.execute("api_legal_case.addCaseMaterial",formData);
    }

    public HashMap<String,Object> getDownFile(final String kid){
        return dao.queryForHashMap("api_legal_case.getDownFile",kid);
    }

    public Integer getOrderStatus(final PageFormData formData){
        return dao.queryForInteger("api_legal_case.getOrderStatus",formData);
    }
}